10. ROS Subscribers

Now that you've written Arm Mover, you have gained an understanding of custom message generation, publishing to a topic, ROS services, parameters, and launch files. Before you are ready to write code, you'll still need to learn to use ROS Subscribers.

ROS Subscribers

A Subscriber enables your node to read messages from a topic, allowing useful data to be streamed into the node. In Python, ROS subscribers frequently have the following format, although other parameters and arguments are possible:

sub1 = rospy.Subscriber("/topic_name", message_type, callback_function)

The "/topic_name" indicates which topic the Subscriber should listen to.

The message_type is the type of message being published on "/topic_name" .

The callback_function is the name of the function that should be called with each incoming message. Each time a message is received, it is passed as an argument to callback_function . Typically, this function is defined in your node to perform a useful action with the incoming data. Note that unlike service handler functions, the callback_function is not required to return anything.

For more information about subscribers, see the documentation here . Let's move on to the look_away node so you can see subscribers in action!

Subscriber quiz

Which of the following ROS nodes would likely need a Subscriber?

SOLUTION:
  • A node for an autonomous vehicle that implements pedestrian detection using camera data.
  • A controller node for a lunar rover which implements the actuation of the throttle and brake given target velocities as input.